<?php

	include_once '../com/mss/model/User.php';
	include '../com/mss/common/Connection.php';
	
	class UserAccessor {
		
		public function save(User $user) {
			$connection = Connection::open();
			$first_name = $user->getFirstName();
			$last_name = $user->getLastName();
			$login_id = $user->getLoginID();
			$password = sha1($user->getPassword(), false);
			$type = $user->getType();
			$mobile = $user->getMobile();
			$email = $user->getEMail();
			
			$query = "INSERT INTO SS_USER (FIRST_NAME, LAST_NAME, LOGIN_ID, PASSWORD, TYPE, MOBILE, EMAIL)
			VALUES ('$first_name' , '$last_name' , '$login_id' ,  '$password', '$type', '$mobile', '$email')";

			mysql_query($query, $connection) or die("error occured whilie inserting data");
			Connection::close($connection);
		}
		
		public function select($id) {
			$connection = Connection::open();
			
			$query = "SELECT * FROM SS_USER WHERE ID = '$id'";
			
			$result = mysql_query($query, $connection) or die("error occured whilie selecting data");
			
			$row = mysql_fetch_assoc($result);
			$first_name = $row['FIRST_NAME'];
			$last_name = $row['LAST_NAME'];
			$type = $row['TYPE'];
			$mobile = $row['MOBILE'];
			$email = $row['EMAIL'];
				
			$user = new User($id, $first_name, $last_name, "", "", $type, $mobile, $email);

			Connection::close($connection);
			
			return $user;			
		}
		
		public function selectAll() {
			$connection = Connection::open();
			
			$query = "SELECT * FROM SS_USER ORDER BY FIRST_NAME";
			
			$result = mysql_query($query, $connection) or die("error occured whilie selecting data");
			
			$users = array();
			while ($row = mysql_fetch_assoc($result)) {
				$id = $row['ID'];
				$first_name = $row['FIRST_NAME'];
				$last_name = $row['LAST_NAME'];
				$login_id = $row['LOGIN_ID'];
				$type = $row['TYPE'];
				$mobile = $row['MOBILE'];
				$email = $row['EMAIL'];
				
				$user = new User($id, $first_name, $last_name, $login_id, "", $type, $mobile, $email);
				array_push($users, $user);
			}
			Connection::close($connection);
			
			return $users;
		}
		
		public function update(User $user) {
			$connection = Connection::open();
			$id = $user->getID();
			$first_name = $user->getFirstName();
			$last_name = $user->getLastName();
			$type = $user->getType();
			$mobile = $user->getMobile();
			$email = $user->getEMail();
			
			$query = "UPDATE SS_USER SET FIRST_NAME = '$first_name',
			 LAST_NAME = '$last_name', TYPE = '$type',
			  MOBILE = '$mobile', EMAIL = '$email' WHERE ID = '$id'";

			mysql_query($query, $connection) or die("error occured whilie updating data");
			Connection::close($connection);			
		}
		
		public function findByLoginIDAndPassword($login_id, $password) {
			$connection = Connection::open();
			
			$query = "SELECT ID FROM SS_USER WHERE LOGIN_ID = '$login_id' AND PASSWORD = '$password'";
			
			$result = mysql_query($query, $connection) or die("error occured whilie selecting data");
			$count = mysql_num_rows($result);
			Connection::close($connection);
			
			return $count;
		}
		
	}

?>